package www.bplead.com.mydemo.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import www.bplead.com.mydemo.R;

/**
 * Created by wangyangzi on 2016/9/26.
 */
public class CustomProgressBar extends View {
    private static final String TAG = "CustomProgressBar";

    //进度条背景颜色
    private int firstColor;
    //进度条滚动颜色
    private int secondColor;
    //进度条界限
    private RectF progressBarRect;
    //圆的宽度
    private int circleWidth;
    //画笔
    private Paint mPaint;
    //进度
    private int progress;
    int pointX;
    int r;

    public CustomProgressBar(Context context) {
        this(context,null);
    }

    public CustomProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray arrs = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar,defStyleAttr,0);
        for(int i=0;i<arrs.getIndexCount();i++){
            int arr = arrs.getIndex(i);
            switch (arr){
                case R.styleable.CustomProgressBar_cpbFirstColor:
                    firstColor = arrs.getColor(arr, Color.BLACK);
                    break;
                case R.styleable.CustomProgressBar_cpbSecondColor:
                    secondColor = arrs.getColor(arr, Color.BLACK);
                    break;
                case R.styleable.CustomProgressBar_cpbCircleWidth:
                    circleWidth = arrs.getDimensionPixelSize(arr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));
                    break;
            }
        }
        arrs.recycle();

        //准备画笔
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(circleWidth);
        mPaint.setStyle(Paint.Style.STROKE);

        new Thread(){
            @Override
            public void run() {
                for(int i=1;i<=360;i++){
                    try {
                        progress = i;
                        postInvalidate();
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //super.onDraw(canvas);
        //获得圆心的x坐标,坐标不是相对于整个屏幕是相对于当前View
        pointX = getWidth() / 2;
        //获得半径
        r = pointX - circleWidth / 2;
        if(progressBarRect == null){
            // 用于定义的圆弧的形状和大小的界限
            progressBarRect = new RectF(pointX  - r,pointX  - r,pointX + r,pointX + r);
        }
        //画出第一个圆
        mPaint.setColor(firstColor);
        canvas.drawCircle(pointX, pointX, r, mPaint); // 画出圆环
        //画出圆弧
        mPaint.setColor(secondColor);
        canvas.drawArc(progressBarRect,-90,progress,false,mPaint);
    }
}
